package com.island.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;


public class FileUtil {
	/**
	 * 读入给定路径文件数据
	 * @param file
	 * @return
	 */
	public static String readFile(File file){
		try{
			 StringBuffer result = new StringBuffer();
	            char buf[] = new char[1024];
	            FileReader reader = new FileReader(file);
	            int len = reader.read(buf);
	            while(len != -1){ //read()方法当读取完最后一个字节再读一遍时，这时候返回的结果是-1
	            	result.append(buf, 0, len);
	            	len = reader.read(buf);
	            }
	            reader.close();
	            return new String(result);
		}catch(Exception e){
			return "";
		}
	}
	/**
	 * 将数据写入指定文件
	 * @param file
	 * @param content
	 */
	public static void writeFile(File file , String content){
		try{
			FileOutputStream fos = new FileOutputStream(file);
			byte [] b = content.getBytes();
			for(int i=0;i<b.length;i++){
				fos.write(b[i]);
			}
			fos.flush();
			fos.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 文件复制
	 * @param srcPath
	 * @param destPath
	 * @return
	 */
	public static boolean copyFile(String srcPath , String destPath){
		try {
			//输入流
			FileInputStream fis = new FileInputStream(srcPath);
			//输出流
			FileOutputStream fos = new FileOutputStream(destPath);
			//输入缓冲流
			BufferedInputStream bis = new BufferedInputStream(fis);
			//输出缓冲流
			BufferedOutputStream bos = new BufferedOutputStream(fos);
			//读入一个字节的数据
			int rd = bis.read();
			while(rd != -1){
				//将内存中的数据写到硬盘文件
				bos.write(rd);
				//继续读入数据
				rd = bis.read();
			}
			bis.close();
			fis.close();
			bos.flush();
			bos.close();
			fos.close();
			return true;
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	/**
	 * 创建文件
	 * @param file
	 * @param isFile  true：是文件  false：是文件夹
	 */
	public static void createFile(File file, boolean isFile) {
		if (!file.exists()) {
			if (!file.getParentFile().exists()) {
				createFile(file.getParentFile(), false);
			} else {
				if (isFile) {
					try {
						file.createNewFile();
					} catch (IOException e) {
						e.printStackTrace();
					}
				} else {
					file.mkdir();
				}
			}
		}
	}
	
	/**
	* 删除文件夹里面的所有文件
	* @param path String 文件夹路径 如 c:/fqf
	*/
	public static void delAllFile(String path) {
		File file = new File(path);
		if (!file.exists()) {
			return;
		}
		if (!file.isDirectory()) {
			return;
		}
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++) {
			if (path.endsWith(File.separator)) {
				temp = new File(path + tempList[i]);
			} else {
				temp = new File(path + File.separator + tempList[i]);
			}
			if (temp.isFile()) {
				temp.delete();
			}
			if (temp.isDirectory()) {
				// delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件
				delFolder(path + File.separatorChar + tempList[i]);// 再删除空文件夹
			}
		}
		System.out.println("删除文件操作 成功执行");
	}
	/**
	 * 删除文件夹
	 */
	public static void delFolder(String folderPath) {
		try {
			delAllFile(folderPath); // 删除完里面所有内容
			String filePath = folderPath;
			filePath = filePath.toString();
			File myFilePath = new File(filePath);
			if (myFilePath.delete()) { // 删除空文件夹
				System.out.println("删除文件夹" + folderPath + "操作 成功执行");
			} else {
				System.out.println("删除文件夹" + folderPath + "操作 执行失败");
			}
		} catch (Exception e) {
			System.out.println("删除文件夹操作出错");
			e.printStackTrace();
		}
	}
	
	public static Map<String, String> readProperties(String fileName)throws IOException{
		
		Map<String, String>map = new HashMap<String, String>();
		if(null!=fileName && !"".equals(fileName)){
				if(!(fileName.endsWith("properties"))){
					fileName+=".properties";
				}
				InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
				Properties propt = new Properties();
				propt.load(inputStream);
				Enumeration enu = propt.keys();
				String k = null;
				String v = null;
				while(enu.hasMoreElements()){
					k = enu.nextElement().toString();
					v = propt.getProperty(k).toString();
					map.put(k, v);
				}
		}
		return map;
	}
	public static void main(String []args){
		//System.out.println(readFile(new File("d:/user.xml")));
		//writeFile(new File("d:/user2.xml"),"xmlxmlxml");
		//copyFile("d:/user.xml","e:/user.xml");
		//createFile(new File("d:/1231.txt"),true);
	}
}
